<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\AppChannel;
use App\Model\AppVersion;
use App\Model\SettingAppHidden;
use App\Util\CityUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class SettingApphiddenController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('App功能隐藏配置');
        $content->body($this->grid());
        return $content;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('创建');
        $content->body($this->form());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new SettingAppHidden());
        $grid->model()->orderBy('id', 'desc');
        // $grid->id('记录ID')->sortable();
        $grid->func_type('屏蔽类型')->display(function ($data) {
            return SettingAppHidden::FUNC[$data] ?? '-';
        });
        $grid->app_id('屏蔽应用')->display(function ($data) {
            if (!$data) return '';
            $data = explode(',', $data);
            foreach ($data as &$item) $item = AppVersion::APPID[$item] ?? '-';
            return implode('，', $data);
        });
        $grid->channel('屏蔽渠道');
        $grid->version('屏蔽版本');
        $grid->begin_time('时间范围')->display(function ($data){
            return $data.'--'.$this->end_time;
        });
        $grid->week('星期')->display(function ($data){
            $str = [];
            $weekArray = ["日", "一", "二", "三", "四", "五", "六"];
            if($data){
                $item = explode(',',$data);
                foreach ($item as $key=>$value){
                    $w = $value == 7 ? 0 : $value;
                    $str[] = '星期'.$weekArray[$w];
                }
            }
            return implode('，',$str);
        });
        $grid->hidden_city('屏蔽城市')->display(function ($data){
            return empty($data) ? '全部' :  $data;
        });
        $grid->hidden_un_know_city('是否屏蔽未知地区')->display(function ($data){
            return $data == 1 ? '是' :  '否';
        });
        $grid->flag('状态')->display(function ($data) {
            return $data == 1 ? '有效' : '无效';
        });
        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableFilter();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            // $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new SettingAppHidden());
        $form->select('func_type', '屏蔽类型')->options(SettingAppHidden::FUNC)->default(1)->required();;
        $form->multipleSelect('app_id', '屏蔽应用')->options(AppVersion::APPID)->default(1)->required();
        $form->select('version', '版本')->options(AppVersion::getAllVersions())->default(-1)->required();
        $form->multipleSelect('channel', '屏蔽渠道')->options(AppChannel::getChannels())->required();
        #$form->select('hidden_city', '版本')->options(AppVersion::hideCity())->default('');
        $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1)->required();
        $form->checkbox('week','星期')->options([1=>'星期一',2=>'星期二',3=>'星期三',4=>'星期四',5=>'星期五',6=>'星期六',7=>'星期天'])->default([1,2,3,4,5,6,7]);
        $form->multipleSelect('hidden_city','屏蔽城市（不选择则全部）')->options(CityUtil::$city);
        $form->switch('hidden_un_know_city', '是否屏蔽未知地区');
        $form->date('begin_time','开始时间')->format('HH:ss')->default('00:00');
        $form->date('end_time','开始时间')->format('HH:ss')->default('23:59');
        return $form;
    }
}
